/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.modules.storage.service.CartService;
import com.jeesite.modules.storage.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 购物车Controller
 */
@RestController(value = "apicart")
@RequestMapping(value = "/api/cart")
public class CartController extends APIController {

    @Autowired
    private CartService cartService;

    /**
     * 查询会员购物车列表
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param size 每页数量
     * @param page 页码
     * @return 会员购物车列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "query")
    @ResponseBody
    public ServerResponse<List<Map>> query(@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId, @RequestParam(value="size",defaultValue = "5") Integer size, @RequestParam(value="page",defaultValue = "1") Integer page)
    {
        return  ServerResponse.createBySuccess(200,cartService.getListByMemberId(transMemberId,memberId,size,page));
    }

    /**
     * 加入购物车
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param transActivityId 转发Id
     * @param goodsSpecId  商品规格Id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "join")
    public ServerResponse<String> join(@RequestParam(value="transMemberId",required = false) String transMemberId,@RequestParam(value="memberId") String memberId,@RequestParam(value="transActivityId") String transActivityId,@RequestParam(value="goodsSpecId") String goodsSpecId) throws Exception {
        Integer code = cartService.join(transMemberId,memberId,transActivityId, goodsSpecId);
        String msg = "";
        switch (code){
            case 200:
                msg = "已成功加入购物车";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }

    /**
     * 删除
     * @param carts 购物车【json】
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "remove")
    public ServerResponse<String> remove(@RequestParam(value="carts") String carts)
    {
        Integer code = cartService.remove(getJsonParam(carts));
        String msg = "";
        switch (code){
            case 200:
                msg = "成功删除";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }

    /**
     * 保存
     * @param cartId 购物车Id
     * @param goodsSpecId 商品规格Id
     * @param num 数量
     * @param remark 备注
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "save")
    public ServerResponse<String> save(@RequestParam(value="cartId") String cartId,@RequestParam(value="goodsSpecId") String goodsSpecId,@RequestParam(value="num") Integer num,@RequestParam(value="remark") String remark)
    {
        Integer code = cartService.save(cartId,goodsSpecId,num,remark);
        String msg = "";
        switch (code){
            case 200:
                msg = "更新成功";
                break;
            default:
                msg = "发生其它异常";
                break;
        }
        return  ServerResponse.createBySuccess(code,msg);
    }
}
